//
//  ExclusiveOperation.m
//  MobileChecker
//
//  Created by Lymons on 15-1-30.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "ExclusiveOperation.h"

#define kDelayInSeconds 0.5

@interface ExclusiveOperation ()

@property (assign, atomic) BOOL isPresentingViewController;
@property (assign, atomic) BOOL isBeginingAction;

@end

@implementation ExclusiveOperation

+ (ExclusiveOperation *)sharedInstance
{
    static dispatch_once_t onceToken;
    static ExclusiveOperation *_sharedInstance = nil;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[[self class] alloc] init];
    });
    return _sharedInstance;
}

+ (id)action:(ActionBlock)block
{
    ExclusiveOperation *eo = [ExclusiveOperation sharedInstance];
    if (eo.isBeginingAction || eo.isPresentingViewController) {
        return nil;
    }
    eo.isBeginingAction = YES;
    
    id ret = block();
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDelayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        eo.isBeginingAction = NO;
    });
    
    return ret;
}

@end
